<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<div id="log"></div>

<form>
<input id="ccname" name="ccname" autocomplete="cc-name" autofocus="">
<input id="cardnumber" name="cardnumber" autocomplete="cc-number">
<input id="cvc" name="cvc" autocomplete="cc-csc">
<input id="ccexp" name="ccexp" autocomplete="cc-exp">
<input type="submit">
</form>
<button onclick="document.forms[0].classList.toggle('hidden')">Hide form</button>
<div id="info" style="position:absolute;left:400px"></div>
<script>
// Setup the JavaScript function that tries to access to the suggested values.
function grabField(field) {
    // Simulate the user attempting to modify the suggested value.
    field.focus();
    field.setSelectionRange(0,0);
    var result = document.execCommand('insertText', false, ' ');

    var val = field.value.trim();
    field.value='';
    var empty_or_initial = val == "" || val == "Homer";
    assert_true(empty_or_initial, 'The JavaScript should not be able to access the suggested values.');
}

// Test that tries to grab the suggested value on empty fields.
test(function() {
    assert_true(window.internals != null, 'This test requires internals object');

    // Set some suggested values.
    var ccname = document.getElementById('ccname');
    var cardnumber = document.getElementById('cardnumber');
    var cvc = document.getElementById('cvc');
    var ccexp = document.getElementById('ccexp');
    internals.setSuggestedValue(ccname, 'suggested ccname');
    internals.setSuggestedValue(cardnumber, 'suggested cardnumber');
    internals.setSuggestedValue(cvc, 'suggested cvc');
    internals.setSuggestedValue(ccexp, 'suggested ccexp');

    // Try to grab the suggested values in the fields.
    grabField(ccname);
    grabField(cardnumber);
    grabField(cvc);
    grabField(ccexp);

}, "JavaScript should not be able to access the suggested values on empty fields.");

// Test that tries to grab the suggested value on a field that contains a value.
test(function() {
    assert_true(window.internals != null, 'This test requires internals object');

    // Setup the JavaScript that adds some values in the fields.
    function addValue(field) {
        // Add some value in the field.
        field.value = "Homer";
    }

    var ccname = document.getElementById('ccname');

    // Add some value in the field.
    ccname.value = "Homer";

    // Set the suggested value.
    internals.setSuggestedValue(ccname, 'suggested ccname');

    // Try to grab the suggested values in the field.
    grabField(ccname);

}, "JavaScript should not be able to access the suggested value on a field that has some value.");

</script>